package gui.components.renderers;

import gfx.hdr.Exposure;
import gfx.hdr.ImageHDR;
import gfx.layers.Layer;
import gui.components.ImageComponent;
import gui.listeners.LayerListItemListener;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

public class LayerListCell extends JPanel{
  private static final long serialVersionUID = 1L;
	private LayerListItemListener layerListItemListener;
	private Layer layer;
	public static final int LIST_ENTRY_HEIGHT = 46;
	
	public LayerListCell(Layer layer){
		this.layer = layer;
		layerListItemListener = new LayerListItemListener(layer);
		initialize();
	}
	
	private void initialize(){
		setLayout(new BorderLayout(4, 4));
		setOpaque(false);
		setBorder(
				new CompoundBorder(
						new MatteBorder(1, 0, 0, 0, Color.WHITE), 
						new CompoundBorder(
								new MatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY), 
								new EmptyBorder(2, 2, 2, 2))));
		
		add(getLeftSidePanel(), BorderLayout.WEST);
		add(getText(), BorderLayout.CENTER);
	}
	
	private JPanel getLeftSidePanel(){
		JPanel leftSidePanel = new JPanel(new BorderLayout(4, 4));
		leftSidePanel.setOpaque(false);
				
		leftSidePanel.add(getLeftVerticalButtonPanel(), BorderLayout.WEST);
		leftSidePanel.add(getThumbnail(), BorderLayout.CENTER);
		
		return leftSidePanel;
	}
	
	private JPanel getLeftVerticalButtonPanel(){
		JPanel leftVerticalButtonPanel = new JPanel(new BorderLayout());
		leftVerticalButtonPanel.add(getShowIcon(), BorderLayout.NORTH);
		leftVerticalButtonPanel.setPreferredSize(new Dimension(16, 16));
		leftVerticalButtonPanel.setOpaque(false);
		return leftVerticalButtonPanel;
	}
	
	private JButton getShowIcon(){
		JButton showIcon = new JButton(new ImageIcon(getClass().getResource("/res/show.png")));
		showIcon.addActionListener(layerListItemListener);
		showIcon.setActionCommand("SHOW");
		showIcon.setBorderPainted(false);
		showIcon.setContentAreaFilled(false);
		showIcon.setFocusable(false);
		return showIcon;
	}
	
	private JPanel getThumbnail(){
		JPanel thumbnail = new ImageComponent(layer.getThumbnail(), layer.getThumbSize(), layer.getThumbSize());
		thumbnail.setLayout(null);
		
		JCheckBox checkBox = new JCheckBox();
		checkBox.setOpaque(false);
		checkBox.setSelected(layer.isSelected());
		checkBox.addChangeListener(layerListItemListener);
		
		thumbnail.add(checkBox);
		
		checkBox.setBounds(
				0,
				layer.getThumbnail().getHeight()  - 19,
				16,
				16);
		
		
		return thumbnail;
	}
	
	private JPanel getText(){
		JPanel textPanel = new JPanel(new BorderLayout(4, 4));
		JLabel layerName = new JLabel(layer.getName());
		JComponent layerProps = null;
		if(layer instanceof ImageHDR){
			JSlider slider = new JSlider(JSlider.HORIZONTAL);
			slider.setMinimum(0);
			slider.setMaximum(255);
			slider.setValue(Exposure.exposureToSlider(((ImageHDR)layer).getExposure()));
			slider.addChangeListener(layerListItemListener);
			slider.addMouseListener(layerListItemListener);
			layerProps = slider;
		}
		else{
			JLabel layerPropsText = new JLabel(layer.getExifShutterSpeed() + "s");
			layerProps = layerPropsText;
		}
		
		textPanel.setOpaque(false);
		layerName.setOpaque(false);
		layerProps.setOpaque(false);
		
		textPanel.add(layerName, BorderLayout.NORTH);
		textPanel.add(layerProps, BorderLayout.CENTER);
		
		return textPanel;
	}
	
	@Override
	public Dimension getPreferredSize(){
		if(getParent() != null){
			return new Dimension(getParent().getBounds().width, LIST_ENTRY_HEIGHT);
		}
		return new Dimension(20, LIST_ENTRY_HEIGHT);
	}
	
	@Override
	public void validate(){
		super.validate();
		Rectangle r = getBounds();
		r.x = 0;
		r.width = getParent().getBounds().width;
		setBounds(r);
	}
}
